package woaini.wind.rest;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import woaini.wind.annotation.Log;
import woaini.wind.domain.MinIOConfig;
import woaini.wind.domain.MinIOContent;
import woaini.wind.service.dto.MinIOQueryCriteria;
import woaini.wind.service.impl.MinIOServiceImpl;
import woaini.wind.utils.PageResult;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/minIOContent")
@Tag(name = "工具：minIO存储管理")
public class MinIOContentController {
  
  private final MinIOServiceImpl minIOService;

  @GetMapping(value = "/config")
  public ResponseEntity<MinIOConfig> queryQiNiuConfig(){
    return new ResponseEntity<>(minIOService.find(), HttpStatus.OK);
  }

  @Log("配置七牛云存储")
  @Operation(summary = "配置七牛云存储")
  @PutMapping(value = "/config")
  public ResponseEntity<Object> updateQiNiuConfig(@Validated @RequestBody MinIOConfig qiniuConfig){
    minIOService.config(qiniuConfig);
    return new ResponseEntity<>(HttpStatus.OK);
  }


  @Operation(summary = "查询文件")
  @GetMapping
  public ResponseEntity<PageResult<MinIOContent>> queryQiNiu(MinIOQueryCriteria criteria, Pageable pageable){
    return new ResponseEntity<>(minIOService.queryAll(criteria,pageable),HttpStatus.OK);
  }

  @Operation(summary = "上传文件")
  @PostMapping
  public ResponseEntity<Object> uploadQiNiu(@RequestParam MultipartFile file){
    MinIOContent minIOContent = minIOService.upload(file,minIOService.find());
    Map<String,Object> map = new HashMap<>(3);
    map.put("id",minIOContent.getId());
    map.put("errno",0);
    map.put("data",new String[]{minIOContent.getUrl()});
    return new ResponseEntity<>(map,HttpStatus.OK);
  }

  @Log("同步七牛云数据")
  @Operation(summary = "同步七牛云数据")
  @PostMapping(value = "/synchronize")
  public ResponseEntity<Object> synchronizeQiNiu(){
    minIOService.synchronize(minIOService.find());
    return new ResponseEntity<>(HttpStatus.OK);
  }

  @Log("下载文件")
  @Operation(summary = "下载文件")
  @GetMapping(value = "/download/{id}")
  public ResponseEntity<Object> downloadQiNiu(@PathVariable Long id){
    Map<String,Object> map = new HashMap<>(1);
    map.put("url", minIOService.download(minIOService.findByContentId(id),minIOService.find()));
    return new ResponseEntity<>(map,HttpStatus.OK);
  }

  @Log("删除文件")
  @Operation(summary = "删除文件")
  @DeleteMapping(value = "/{id}")
  public ResponseEntity<Object> deleteQiNiu(@PathVariable Long id){
    minIOService.delete(minIOService.findByContentId(id),minIOService.find());
    return new ResponseEntity<>(HttpStatus.OK);
  }

  @Log("删除多张图片")
  @Operation(summary = "删除多张图片")
  @DeleteMapping
  public ResponseEntity<Object> deleteAllQiNiu(@RequestBody Long[] ids) {
    minIOService.deleteAll(ids, minIOService.find());
    return new ResponseEntity<>(HttpStatus.OK);
  }
  
}
